package com.ddwl.order.controller;


import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Submit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.order.dao.UpdateAfterSaleOrderBo;
import com.ddwl.order.service.AfterSaleOrderServer;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.order.ConfirmAfterSaleBo;
import com.ddwl.schema.bo.order.CreateAfterSaleOrderBo;
import com.ddwl.schema.bo.order.QueryAfterSaleOrderListBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.order.AfterSaleOrderCheckVo;
import com.ddwl.schema.vo.order.AfterSaleOrderDetailVo;
import com.ddwl.schema.vo.order.AfterSaleOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * 售后相关接口
 *
 * @author wenbin
 */
@Api(tags = "售后")
@RestController
@RequestMapping("/afterSale")
public class AfterSaleController extends BaseController {

    @Resource
    private AfterSaleOrderServer afterSaleOrderServer;

    @Submit
    @PostMapping("/createAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("创建售后申请")
    public RestVo<String> createAfterSaleOrder(@Valid @RequestBody CreateAfterSaleOrderBo bo) throws Exception {
        return RestVo.ok(afterSaleOrderServer.create(bo, getUserId(), getUserNameNew()));
    }

    @Submit
    @PostMapping("/updateAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("修改售后申请")
    public RestVo updateAfterSaleOrder(@Valid @RequestBody UpdateAfterSaleOrderBo bo) {
        afterSaleOrderServer.updateAfterSale(bo, getUserId());
        return RestVo.ok();
    }

    @PostMapping("/queryUserAfterSaleList")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("查询我的售后订单列表,app小程序")
    public RestVo<PageVo<AfterSaleOrderVo>> queryUserAfterSaleOrder(@Valid @RequestBody PageBo<QueryAfterSaleOrderListBo> pageBo) {
        pageBo.getParam().setUserId(getUserId());
        pageBo.getParam().setPlatformTag(0);
        return RestVo.ok(afterSaleOrderServer.getPage(pageBo));
    }

    @PostMapping("/queryConsoleAfterSaleList")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.API})
    @ApiOperation("查询售后订单列表,后台")
    public RestVo<PageVo<AfterSaleOrderVo>> queryConsoleAfterSaleOrder(@Valid @RequestBody PageBo<QueryAfterSaleOrderListBo> pageBo) {
        pageBo.getParam().setPlatformTag(0);
        return RestVo.ok(afterSaleOrderServer.getPage(pageBo));
    }

    @GetMapping("/queryConsoleAfterSaleDetail/{afterId}")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("查询售后订单明细")
    public RestVo<AfterSaleOrderDetailVo> queryConsoleAfterSaleDetail(@PathVariable String afterId) {
        return RestVo.ok(afterSaleOrderServer.getAfterSaleOrderDetail(afterId));
    }

    @PostMapping("/confirmAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("审批售后订单")
    public RestVo<AfterSaleOrderVo> confirmAfterSaleOrder(@Valid @RequestBody ConfirmAfterSaleBo bo) {
        afterSaleOrderServer.confirmAfterSale(bo);
        return RestVo.ok();
    }

    @GetMapping("/receivedGood/{afterId}")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("官方收货")
    public RestVo<AfterSaleOrderVo> receivedGood(@PathVariable String afterId) {
        afterSaleOrderServer.receivedGood(afterId);
        return RestVo.ok();
    }

    @PostMapping("/rejectAfterSale")
    @ApiOperation("拒绝售后订单")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<AfterSaleOrderVo> rejectAfterSaleOrder(@Valid @RequestBody ConfirmAfterSaleBo bo) {
        afterSaleOrderServer.rejectAfterSaleOrder(bo);
        return RestVo.ok();
    }

    @PostMapping("/cancelAfterSale")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("用户取消售后订单")
    public RestVo cancelAfterSaleOrder(@RequestBody ConfirmAfterSaleBo bo) {
        afterSaleOrderServer.cancelAfterSaleOrder(bo, getUserId());
        return RestVo.ok();
    }

    @PostMapping("/updateRemark")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("更新售后订单备注")
    public RestVo updateRemark(@RequestParam ConfirmAfterSaleBo bo) {
        afterSaleOrderServer.updateRemark(bo);
        return RestVo.ok();
    }

    @GetMapping("/getAfterReason")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("获取售后原因")
    public RestVo<Map<String, String>> getAfterReason() {
        Map<String, String> afterReason = afterSaleOrderServer.getAfterReason();
        return RestVo.ok(afterReason);
    }

    @GetMapping("/afterSaleRefund/{afterId}")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("退款")
    public RestVo afterSaleRefund(@PathVariable String afterId) {
        afterSaleOrderServer.refund(afterId, getUserId(), getUserNameNew());
        return RestVo.ok();
    }

    @GetMapping("/checkAfterSale/{spuOrderId}")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("检测售后")
    public RestVo<AfterSaleOrderCheckVo> checkAfterSale(@PathVariable String spuOrderId) {
        AfterSaleOrderCheckVo afterSaleOrderCheckVo = afterSaleOrderServer.checkAfterSale(spuOrderId);
        return RestVo.ok(afterSaleOrderCheckVo);
    }
}
